package com.dwxt.cdms.util.wx;

import com.alibaba.fastjson.JSONObject;
import com.dwxt.cdms.util.RedisUtil;
import com.dwxt.common.util.HttpUtils;
import com.dwxt.common.util.JsonUtils;
import com.dwxt.common.util.OkHttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;

@Slf4j
public class WxUtil {


    static final String appSecret="0566d1ad14b0bcd9eba4d1dc7fbaedaa";
    static final String appId="wxfcc4d2ccf2c52016";
    static final String template_id_cl="Z43mRCS35XXZnEotdOTvlBZ3EQSIQMbp-Zw387SNVxc";
    /**
     * 给公众号推送抛物信息
     * 属性	类型	默认值	必填	说明
     * access_token	string		是	接口调用凭证
     * touser	string		是	接收者（用户）的 openid
     * template_id	string		是	所需下发的订阅模板id
     * page	string		否	跳转网页时填写
     * miniprogram	Array.<Objtect>		否	跳转小程序时填写，格式如{ "appid": "pagepath": { "value": any } }
     * data	Object		是	模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
     */
    public static void pushMsgToWx(String openId){

        HashMap<String, Object> param = new HashMap<>();
        JSONObject top = new JSONObject();
        JSONObject first = new JSONObject();
        first.put("value","您收到一条SOS处理结果提醒");
        top.put("first",first);
        //家人姓名
        JSONObject keyword1 = new JSONObject();
        keyword1.put("value","camera.getName()");
        top.put("keyword1",keyword1);
        //设备编号
        JSONObject keyword2 = new JSONObject();
        keyword2.put("value","camera.getSerial()");
        top.put("keyword2",keyword2);

        //remark
        JSONObject remark = new JSONObject();
        remark.put("value","点此消息可查看抛物视频");
        top.put("remark",remark);
        param.put("touser",openId);
        param.put("template_id",template_id_cl);
        param.put("data",top);
        param.put("url","http://118.178.180.213/wssq/parabolic/app/index.html?id=");
        System.out.println(JsonUtils.objectToJson(param));
        String s = OkHttpUtils.jsonPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + getAccessToken(), JsonUtils.objectToJson(param), null);
        log.info("发送模板返回："+s);
    }

    /**
     * 获取access_token
     */
    public static String getAccessToken(){
        String wx_access_token = RedisUtil.get("wx_access_token", String.class);
        if (!StringUtils.isNotEmpty(wx_access_token)){
            return refreshAccessToken();
        }
        return wx_access_token;
    }

    /**
     * 刷新access_token
     */
    private static String refreshAccessToken(){
        HashMap<String, Object> param = new HashMap<>();
        param.put("grant_type","client_credential");
        param.put("appid",appId);
        param.put("secret",appSecret);
        String request = HttpUtils.getRequest("https://api.weixin.qq.com/cgi-bin/token", param);
        JSONObject jsonObject1 = JSONObject.parseObject(request);
        String access_token = jsonObject1.get("access_token").toString();
        RedisUtil.set("abx_wx_access_token",access_token,Long.parseLong(jsonObject1.get("expires_in").toString())-5);
        log.info("gkpw_wx_access_token:"+access_token);
        log.info("获取access_token："+request);
        return access_token;
        //{"errcode":40164,"errmsg":"invalid ip 183.230.62.124 ipv6 ::ffff:183.230.62.124, not in whitelist rid: 61ef5f88-25bbc945-1ae52967"}
    }

}
